package com.zyh.homework;

import java.util.Random;

public class Test2 {
    public static void main(String[] args) {
        /*
        将数字1-10保存到一个长度为10的一维数组中
        定义一个新数组,长度为3,取出原来数组中随机三个元素(不考虑是否重复)
        1.给新数组的元素赋值
        2.求新数组所有元素的和

         */
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] newArr = new int[3];
        int sum = 0;
        for (int i = 0; i < newArr.length; i++) {
            int index = new Random().nextInt(arr.length);
            newArr[i] = arr[index];
            sum = sum + newArr[i];
        }
        for (int i = 0; i < newArr.length; i++) {
            System.out.print(newArr[i] + " ");
        }
        System.out.println();
        System.out.println("sum = " + sum);
    }
}
